/**
 * 题目数据模型
 */
module.exports = class Article extends require('./model') {

    /**
     * 获取题目列表
     */
    static getList() {
        return new Promise((resolve, reject) => {
            let sql = 'SELECT id,title,content,`time`,`code`,major FROM article WHERE `code`=1 ORDER BY TIME DESC'
            this.query(sql).then(results => {
                resolve(results)
            }).catch(err => {
                console.log(`获取题目列表失败：${err.message}`)
                reject(err)
            })
        })
    }
    /**
     * 获取老师题目列表
     */
    static getListByTid(tid) {
        return new Promise((resolve, reject) => {
            let sql = 'SELECT id,title,content,`code` FROM article WHERE tid = ? ORDER BY TIME DESC'
            this.query(sql, tid).then(results => {
                resolve(results)
            }).catch(err => {
                console.log(`获取题目列表失败：${err.message}`)
                reject(err)
            })
        })
    }

    /**
     * 获取指定类目下的题目列表
     * @param {integer}} id 类目编号
     */
    static getListByCategoryId(id) {
        return new Promise((resolve, reject) => {
            let sql = 'SELECT id,title,content,`time`,major FROM article WHERE category_id = ? AND `code`=1 ORDER BY TIME DESC'
            this.query(sql, id).then(results => {
                resolve(results)
            }).catch(err => {
                console.log(`获取指定类目下的题目列表失败：${err.message}`)
                reject(err)
            })
        })
    }


    /**
     * 获取指定关键词的题目列表
     * @param {string}} keyword 关键词
     */
    static getListBykeywrod(keyword) {
        return new Promise((resolve, reject) => {
            let sql = 'SELECT id,title,content,`time`,major FROM article WHERE title LIKE ? AND `code`=1 ORDER BY TIME DESC'
            this.query(sql, `%${keyword}%`).then(results => {
                resolve(results)
            }).catch(err => {
                console.log(`获取指定关键词的题目列表失败：${err.message}`)
                reject(err)
            })
        })
    }

    /**
     * 获取指定题目的详情
     * @param {integer} id 题目编号
     */
    static getArticleById(id) {
        return new Promise((resolve, reject) => {
            let sql = 'SELECT a.id,a.title,a.major,a.code,a.content,a.`time`,a.`category_id`,a.`hot`,c.`name`,a.`tid`,a.`uid`,t.`tname`,t.`ttitle`,t.`other`,t.`tphone` FROM article a,category c,teacher t WHERE a.`category_id` = c.`id` AND a.`tid` = t.`id` AND a.id = ?'
            this.query(sql, id).then(results => {
                resolve(results[0])
            }).catch(err => {
                console.log(`获取指定题目的详情失败：${err.message}`)
                reject(err)
            })
        })
    }

    /**
     * 上一篇题目
     * @param {integer} id 当前题目的编号
     */
    static getPrevArticle(id) {
        return new Promise((resolve, reject) => {
            let sql = 'SELECT id,title FROM article WHERE id < ? ORDER BY id DESC LIMIT 1'
            this.query(sql, id).then(results => {
                resolve(results[0])
            }).catch(err => {
                console.log(`上一篇题目失败：${err.message}`)
                reject(err)
            })
        })
    }

    /**
     * 下一篇题目
     * @param {integer} id 当前题目的编号
     */
    static getNextArticle(id) {
        return new Promise((resolve, reject) => {
            let sql = 'SELECT id,title FROM article WHERE id > ? ORDER BY id ASC LIMIT 1'
            this.query(sql, id).then(results => {
                resolve(results[0])
            }).catch(err => {
                console.log(`下一篇题目失败：${err.message}`)
                reject(err)
            })
        })
    }

    /**
     * 总博文数
     */
    static getCount(category_id, hot, code) {
        return new Promise((resolve, reject) => {
            let sql = 'SELECT COUNT(1) AS `count` FROM article WHERE 1=1'

            sql += category_id != -1 && category_id ? ` AND category_id=${category_id}` : ''
            sql += hot != -1 && hot ? ` AND hot=${hot}` : ''
            sql += code != -1 && code ? ` AND code=${code}` : ''

            this.query(sql).then(results => {
                resolve(results[0].count)
            }).catch(err => {
                console.log(`获取总博文数失败：${err.message}`)
                reject(err)
            })
        })
    }
    /**
     * 个人出题数
     */
    static getArticleCount(tid) {
        return new Promise((resolve, reject) => {
            let sql = 'SELECT COUNT(1) AS `myArticleCount` FROM article WHERE tid = ?'
            this.query(sql,tid).then(results => {
                resolve(results[0].myArticleCount)
            }).catch(err => {
                console.log(`获取总博文数失败：${err.message}`)
                reject(err)
            })
        })
    }
    /**
     * 获取选题学生列表
     */
    static getStudentsList(id) {
        return new Promise((resolve, reject) => {
            let sql = 'SELECT u.uid,u.username,u.account FROM `user` u,article a WHERE (u.aid=a.id OR u.aid2=a.id OR u.aid3=a.id) AND a.id = ? ORDER BY u.account ASC'
            this.query(sql,id).then(results => {
                resolve(results)
            }).catch(err => {
                console.log(`获取选题学生列表失败：${err.message}`)
                reject(err)
            })
        })
    }

    /**
     * 获取指定页题目列表
     * @param {integer} start 起始索引
     * @param {integer} size 查询条目数
     */
    static getPage(start, size, category_id, hot, code) {
        return new Promise((resolve, reject) => {
            let sql = 'SELECT id,title,hot,`code` FROM article WHERE 1=1'

            sql += category_id != -1 && category_id ? ` AND category_id=${category_id}` : ''
            sql += hot != -1 && hot ? ` AND hot=${hot}` : ''            
            sql += code != -1 && code ? ` AND code=${code}` : ''

            sql += ' ORDER BY `time` DESC LIMIT ?,?'
            this.query(sql, [start, size]).then(results => {
                resolve(results)
            }).catch(err => {
                console.log(`获取指定页题目列表失败：${err.message}`)
                reject(err)
            })
        })
    }

    /**
     * 设置热门
     * @param {integer} id 题目编号
     * @param {integer} hot 热门状态
     */
    static setHot(id, hot) {
        return new Promise((resolve, reject) => {
            let sql = 'UPDATE article SET hot = ? WHERE id = ?'
            this.query(sql, [hot, id]).then(results => {
                resolve(results.affectedRows)
            }).catch(err => {
                console.log(`设置热门失败：${err.message}`)
                reject(err)
            })
        })
    }
    

    /**
     * 添加题目
     * @param {Object} article 题目对象
     */
    static add(article) {
        return new Promise((resolve, reject) => {
            let sql = 'INSERT INTO article SET ?'
            this.query(sql, article).then(results => {
                resolve(results.insertId)
            }).catch(err => {
                console.log(`添加题目失败：${err.message}`)
                reject(err)
            })
        })
    }

    /**
     * 删除题目
     * @param {integer}} id 题目编号
     */
    static del(id) {
        return new Promise((resolve, reject) => {
            let sql = 'DELETE FROM article WHERE id = ?'
            this.query(sql, id).then(results => {
                resolve(results.affectedRows)
            }).catch(err => {
                console.log(`删除题目失败：${err.message}`)
                reject(err)
            })
        })
    }

    /**
     * 编辑题目
     * @param {Object} article 题目对象
     */
    static edit(article) {
        return new Promise((resolve, reject) => {
            let sql = 'UPDATE article SET title = ?, major = ?, tid = ?, content = ?, hot = ?, category_id = ?, code = ?, uid = ? WHERE id = ?'
            this.query(sql, [article.title, article.major, article.tid, article.content, article.hot, article.category_id, article.code, article.uid, article.id]).then(results => {
                resolve(results.affectedRows)
            }).catch(err => {
                console.log(`编辑题目失败：${err.message}`)
                reject(err)
            })
        })
    }

    /**
     * 反选学生
     * @param {Object} article 题目对象
     */
    static setStudent(article) {
        return new Promise((resolve, reject) => {
            let sql = 'UPDATE article SET code = ?, uid = ? WHERE id = ?'
            this.query(sql, [article.code, article.uid, article.id]).then(results => {
                resolve(results.affectedRows)
            }).catch(err => {
                console.log(`反选学生失败：${err.message}`)
                reject(err)
            })
        })
    }

    /* 
        检查是否重复
    */
    static checkUid(uid){
        return new Promise((resolve,reject)=>{
            let sql = 'SELECT count(*) FROM article WHERE uid = ?'
            this.query(sql,uid).then(results => {
                resolve(results[0].count)
            }).catch(err => {
                console.log(`检查是否重复失败：${err.message}`)
                reject(err)
            })
        })
    }
}